package anaydis.animation.sort.gui;

import anaydis.animation.core.AnimationController;
import anaydis.animation.model.SortDataModel;
import anaydis.animation.sort.SortAnimation;
import com.sun.istack.internal.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

class SortToolBar
        extends JToolBar
{
    private JComboBox sortChoices;
    private JComboBox dataChoices;
    private JSlider speedSlider;
    private AbstractButton runButton;
    private AbstractButton stopButton;

    public SortToolBar(@NotNull final JComboBox sortChoices)
    {
        super(JToolBar.HORIZONTAL);

        this.sortChoices = sortChoices;
        add(sortChoices);

        final Vector<SortDataModel> models = new Vector<SortDataModel>(SortAnimation.getModels());
        dataChoices = new JComboBox(models);
        dataChoices.setMaximumRowCount(models.size());
        add(dataChoices);

        runButton = makeButton("run", "pause", AnimationController.RUN_COMMAND);
        stopButton = makeButton("stop", null, AnimationController.STOP_COMMAND);
        addSeparator();
        speedSlider = new JSlider(0, AnimationController.MAX_SPEED, AnimationController.MAX_SPEED / 4);
        speedSlider.setMajorTickSpacing(AnimationController.MAX_SPEED / 4);
        speedSlider.setMinorTickSpacing(1);
        speedSlider.setPaintTicks(true);
        add(speedSlider);

        setFloatable(false);
        setRollover(true);
    }

    public void setControler(AnimationController animationController)
    {
        speedSlider.addChangeListener(animationController);
        runButton.addActionListener(animationController);
        stopButton.addActionListener(animationController);
        dataChoices.addItemListener(animationController);
        sortChoices.addItemListener(animationController);
    }

    public Dimension getMaximumSize()
    {
        return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
    }


    void enableControls(boolean running)
    {
        dataChoices.setEnabled(!running);
        if (!running) {
            runButton.setSelected(false);
        }
        stopButton.setEnabled(running);
    }

    protected AbstractButton makeButton(String imageName, String selectedImageName, String actionCommand)
    {
        //Create and initialize the button.
        AbstractButton button;
        if (selectedImageName == null) {
            button = new JButton();
        }
        else {
            button = new JToggleButton();
            button.setSelectedIcon(getImageIcon(selectedImageName));
        }
        button.setActionCommand(actionCommand);
        button.setIcon(getImageIcon(imageName));
        button.setBorderPainted(false);
        add(button);
        return button;
    }

    private ImageIcon getImageIcon(String imageName)
    {
        return new ImageIcon(getClass().getResource("images/" + imageName + ".png"));
    }

    public SortDataModel getData()
    {
        return (SortDataModel) dataChoices.getSelectedItem();
    }

    public JSlider getSlider()
    {
        return speedSlider;
    }

    public void pause()
    {
        runButton.setSelected(false);
    }
}